var $ = require('../../common/helper.js');
Page({
	data: {
		city_id: 0,
		person_id: 0,
		years: [],
		year: '',
		month: '',
		months: ''
	},
	onLoad: function (options) {
		if (typeof (options.city_id) == 'undefined' || !options.city_id.length ||
			typeof (options.person_id) == 'undefined' || !options.person_id.length) {
			$.overloadError('请先选择参保人与参保地');
			wx.navigateBack();
			return;
		}
		var _this = this;
		$.get('api.php?app=cart&act=month&city_id=' + options.city_id + '&person_id=' + options.person_id, function (json) {
			var years = json.data.years;
			for (var i = 0; i < years.length; i++) {
				years[i].view_x = '';
			}
			_this.setData({
				city_id: options.city_id,
				person_id: options.person_id,
				years: years,
				year: json.data.year,
				month: json.data.month
			});
		});
	},
	totalIndex: function () {
		var years = this.data.years, total = 0;
		for (var i = 0; i < years.length; i++) {
			if (years[i].view_x.length) total++;
		}
		return total;
	},
	firstIndex: function () {
		var years = this.data.years, index = -1;
		for (var i = 0; i < years.length; i++) {
			if (years[i].view_x.length) {
				index = i;
				break;
			}
		}
		return index;
	},
	lastIndex: function () {
		var years = this.data.years, index = -1;
		for (var i = years.length - 1; i >= 0; i--) {
			if (years[i].view_x.length) {
				index = i;
				break;
			}
		}
		return index;
	},
	setViewX: function (firstIndex, lastIndex) {
		var years = this.data.years;
		for (var i = 0; i < years.length; i++) {
			if (firstIndex <= i && i <= lastIndex) {
				years[i].view_x = 'view-x';
			} else {
				years[i].view_x = '';
			}
		}
		return years;
	},
	select: function (e) {
		var years = this.data.years, d = e.currentTarget.dataset,
			year = d.year, month = d.month, index = Number(d.index);
		if (index != 0 && !years[0].view_x.length) return;
		if (index == 0 && years[0].view_x.length) return;
		if (!this.totalIndex()) {
			years[index].view_x = 'view-x';
		} else {
			var firstIndex = this.firstIndex(), lastIndex = this.lastIndex();
			if (index == firstIndex && index == lastIndex) {
				years[index].view_x = '';
			} else if (index == firstIndex || index == lastIndex) {
				years[index].view_x = '';
			} else if (index < firstIndex) {
				years = this.setViewX(index, lastIndex);
			} else if (index > lastIndex) {
				years = this.setViewX(firstIndex, index);
			} else {
				years = this.setViewX(firstIndex, index);
			}
		}
		this.setData({
			years: years
		});
	},
	submit: function () {
		var months = [];
		var years = this.data.years;
		for (var i = 0; i < years.length; i++) {
			if (years[i].view_x.length) {
				months.push(years[i].year + '-' + years[i].month);
			}
		}
		if (!months.length) {
			$.overloadError('请选择参保月份');
			return;
		}
		$.get('api.php?app=cart&act=check_min_months', { city_id: this.data.city_id, person_id: this.data.person_id, months: months.join(',') }, function (json) {
			var apps = $.getApps(), app = apps[apps.length - 2];
			app.setParam('month', months);
			wx.navigateBack();
		});
	}
})